Option Strict Off
Option Explicit On
Imports VB = Microsoft.VisualBasic
Imports Microsoft.VisualBasic.PowerPacks
Namespace BMLERP
	Friend Class frmPUR500
		Inherits System.Windows.Forms.Form
		Dim CustType(3) As Object
		Dim StaOrd(6) As Object
		Dim POType(3) As Object
		Dim OldOHPON As Object
		Dim Per(3) As String
		Dim NewTrans As Byte
		Dim WithEvents rst As ADODB.Recordset
		Dim sql As String
		Dim oldSave, oldNewItem, oldAddNew, oldReview, oldPreview, oldEdit, oldCancel As Object
		Dim olddelline As Boolean

		Function Check() As Object
			Dim MsgResult As Object
			Dim recPOH_POL As ADODB.Recordset
			Check = True
			If NewTrans Then ' nhap moi them vao POH
				rst = New ADODB.Recordset
				sql = "SELECT OHPON FROM POH WHERE OHPON= '" & Trim(txtOHPON.Text) & "'"
				rst.Open(sql, ConnectString, ADODB.CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockOptimistic)
				If rst.RecordCount <> 0 Then
					MsgBox("The Order " & Trim(txtOHPON.Text) & " is Already exist")
					Check = False
					txtOHPON.Focus()
					Exit Function
				End If
				'Kiem tra status khi sua chua du lieu
			Else
				sql = "SELECT OHPOS,OLSTT FROM POH JOIN POL ON POH.OHPON=POL.OLPON WHERE OHPON= '" & Trim(txtOHPON.Text) & "'" & " AND POL.OLPOL='" & txtOLPOL.Text & "'"
				recPOH_POL = DBconn.Execute(sql)
				'rst.Open SQL, ConnectString, adOpenKeyset, adLockOptimistic
				If Not recPOH_POL.EOF Then
					If recPOH_POL.Fields("OHPOS").Value = 5 Then ' kiem tra toan bo Order
						MsgResult = MsgBox("This Order has finished.You can't change data", MsgBoxStyle.OkOnly, "Warning")
						Check = False
						Exit Function
					End If

					If recPOH_POL.Fields("OLSTT").Value = 5 Then
						MsgResult = MsgBox("This item has finished.You can't change data", MsgBoxStyle.OkOnly, "Warning")
						Check = False
						Exit Function

					End If
				End If
			End If

			If txtOHPON.Text = "" Then
				MsgBox("You must enter Purchase Order Number", MsgBoxStyle.OkOnly, "Error entry")
				Check = False
				Exit Function
			End If
			'Ktra so Purchase Order
			If Not IsNumeric(txtOHPON.Text) Then
				MsgBox("Input string of Digit", MsgBoxStyle.Critical, "Warning")
				Check = False
				Exit Function
			End If

			'    If txtOHCNO.Text = "" Then
			'           MsgBox "You must enter Customer No", vbOKOnly, "Error entry"
			'           Check = False
			'           Exit Function
			'    End If
			'
			If (txtOHPOD.Text = "") Or (Not IsDate(txtOHPOD.Text)) Then
				MsgBox("You must enter Purchase Order Date", MsgBoxStyle.OkOnly, "Error entry")
				Check = False
				Exit Function
			End If

			If (txtOHRQD.Text <> "") And (Not IsDate(txtOHPOD.Text)) Then
				MsgBox("Purchase Order Date Incorrect", MsgBoxStyle.OkOnly, "Error entry")
				Check = False
				Exit Function
			End If

			If (txtOLODQ.Text = "") Or (txtOLODW.Text = "") Then
				MsgBox("You must enter values in Quantity PO/RC or Weighr PO/RC textboxt", MsgBoxStyle.OkOnly, "Error entry")
				Check = False
				Exit Function
			End If
			If Not (IsNumeric(txtOLODQ.Text) And IsNumeric(txtOLODW.Text)) Then
				MsgBox("You must enter number values in Quantity PO/RC or Weighr PO/RC textboxt", MsgBoxStyle.OkOnly, "Error entry")
				Check = False
				Exit Function
			End If

		End Function

		Private Sub cboOHCNO_ClickEvent(ByVal eventSender As System.Object, ByVal eventArgs As AxMSDataListLib.DDataComboEvents_ClickEvent) Handles cboOHCNO.ClickEvent
			If eventArgs.area = 2 Then
				txtOHCNO.Text = cboOHCNO.BoundText
				txtCMNME.Text = cboOHCNO.CtlText
				cboOHCNO.Visible = False
			End If
		End Sub

		Private Sub cboOHCPO_SelectedIndexChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cboOHCPO.SelectedIndexChanged
			'txtOHVDN.SetFocus
		End Sub

		Private Sub cboOHCUR_ClickEvent(ByVal eventSender As System.Object, ByVal eventArgs As AxMSDataListLib.DDataComboEvents_ClickEvent) Handles cboOHCUR.ClickEvent
			If eventArgs.area = 2 Then
				txtOHCUR.Text = cboOHCUR.BoundText
				txtOHEXR.Text = Find("CCI", "CICCR", " where CICCD='" & txtOHCUR.Text & "'")
			End If
		End Sub

		Private Sub cboOHVDN_ClickEvent(ByVal eventSender As System.Object, ByVal eventArgs As AxMSDataListLib.DDataComboEvents_ClickEvent) Handles cboOHVDN.ClickEvent
			If eventArgs.area = 2 Then
				txtOHVDN.Text = cboOHVDN.BoundText
				txtVMNME.Text = cboOHVDN.CtlText
				cboOHVDN.Visible = False
			End If
		End Sub

		Private Sub cboOLITN_ClickEvent(ByVal eventSender As System.Object, ByVal eventArgs As AxMSDataListLib.DDataComboEvents_ClickEvent) Handles cboOLITN.ClickEvent
			If eventArgs.area = 2 Then
				txtOLITN.Text = cboOLITN.BoundText
				txtOLITD.Text = cboOLITN.CtlText
				cboOLITN.Visible = False
			End If
		End Sub

		Private Sub cboOLPER_SelectedIndexChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cboOLPER.SelectedIndexChanged
			Select Case cboOLPER.SelectedIndex + 1
				Case 1
					txtOLLAM.Text = CStr(CInt(Val(txtOLODQ.Text) * (Val(txtOLUMP.Text) - (Val(txtOLUMP.Text) * Val(txtOLPDC.Text) / 100))))
					txtOLBLA.Text = CStr(Val(txtOLODQ.Text) * (Val(txtOLUMP.Text) - (Val(txtOLUMP.Text) * Val(txtOLPDC.Text) / 100)) * Val(txtOHEXR.Text))
				Case 2
					txtOLLAM.Text = CStr(CInt(Val(txtOLODW.Text) * (Val(txtOLUMP.Text) - (Val(txtOLUMP.Text) * Val(txtOLPDC.Text) / 100))))
					txtOLBLA.Text = CStr(Val(txtOLODW.Text) * (Val(txtOLUMP.Text) - (Val(txtOLUMP.Text) * Val(txtOLPDC.Text) / 100)) * Val(txtOHEXR.Text))
			End Select
		End Sub

		Private Sub cmdAddnew_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdAddnew.Click
			NewTrans = True
			ISADDING = True
			ISEDITING = True
			WriteToScreen()
			ButtonStatus(("edit"))
			txtOHPON.Focus()
		End Sub

		Private Sub CmdCancel_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles CmdCancel.Click
			NewTrans = False
			ISADDING = False
			ISEDITING = False
			WriteToScreen()
			ButtonStatus(("cl"))
		End Sub

		Private Sub cmdCloseLine_Click()
			Dim recPOL As ADODB.Recordset
			Dim Unt, Item, Line, WGT, Status As Object
			Line = txtOLPOL.Text
			Item = txtOLITN.Text
			Status = cboOLSTT.SelectedIndex + 1
			If (cboOHPOS.SelectedIndex + 1) = 6 Then
				MsgBox("This Purchase Order had been cancel")
				Exit Sub
			Else
				If Status = 6 Then
					MsgBox("This Item had been deleted")
				Else
					If Status = 5 Then
						MsgBox("This Item had been closed before")
					Else
						CloseLine(Item, Line, WGT, Unt, Status)
					End If
				End If
				ButtonStatus(("clne"))
				WriteToScreen()
			End If
		End Sub

		Private Sub cmdClosePO_Click()
			ClosePO()
			ButtonStatus(("CP"))
			WriteToScreen()
		End Sub

		Private Sub cmddelline_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmddelline.Click
			'    NewTrans = False
			'    ISADDING = False
			'    ISEDITING = False
			'    ISDELETED = True
			'    WriteToScreen
			Dim vb As Object
			If (txtOHPON.Text <> "") And (txtOLITN.Text <> "") Then
				sql = ""
				sql = "select olstt from poh,pol where pol.olitn='" & Trim(txtOLITN.Text) & "' and olstt='1' and poh.ohpos='1'"
				rst = New ADODB.Recordset
				rst.Open(sql, ConnectString, ADODB.CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockOptimistic)
				If rst.RecordCount > 0 Then
					vb = MsgBox(" Do you want delete this line ?", MsgBoxStyle.DefaultButton2 + MsgBoxStyle.OkCancel, "Deleting...")
					If vb = MsgBoxResult.Ok Then
						sql = ""
						sql = "delete from POL where OLPON='" & Trim(txtOHPON.Text) & "' AND  OLITN='" & Trim(txtOLITN.Text) & "'"
						DBconn.Execute(sql)
						WriteToScreen()
					Else
						Exit Sub
					End If
				Else
					MsgBox("Can not delete this line !", MsgBoxStyle.Critical, "Warning")
					Exit Sub
				End If
			Else
				MsgBox("The TextBox Order No or Item No is Empty...!", MsgBoxStyle.Critical, "Warning")
			End If 'ket thuc delete
			ButtonStatus(("cl"))


		End Sub
		Private Sub cmdEdit_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdEdit.Click
			ISEDITING = True
			'WriteToScreen
			ButtonStatus(("edit"))
		End Sub

		Private Sub cmdexit_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdexit.Click
			Me.Close()
		End Sub

		Private Sub cmdNewItem_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdNewItem.Click
			'NewTrans = True
			If txtOHPON.Text <> "" Then
				ISADDING = True
				ISEDITING = True
				WriteToScreen()
				ButtonStatus(("edit"))
			Else
				MsgBox("Not yet Purchase Order")
			End If
		End Sub

		Private Sub CmdPreview_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles CmdPreview.Click
			Dim ReportName As String
			ButtonStatus(("PRE"))
			ReportName = "request.rpt"
			If txtOHPON.Text = "" Or IsDBNull(txtOHPON.Text) Then
				MsgBox("You must chose Purchase Order No.")
				Exit Sub
			Else
				CrystalReport1.ReportFileName = DDAN & "\" & ReportName
				CrystalReport1.RetrieveStoredProcParams()
				CrystalReport1.set_StoredProcParam(0, txtOHPON.Text)
				CrystalReport1.Action = 1
			End If
		End Sub

		Private Sub cmdReview_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdReview.Click
			'Dim myform As New frmReview
			'myform.Tag = "PUR500"
			'myform.Show()
			frmReview.Tag = "PUR500"
			frmReview.ShowDialog()
			ISEDITING = False
			ISADDING = False
			If frmReview.Tag <> "" Then
				txtOHPON.Text = frmReview.Tag
				frmReview.Close()
				'If myform.Tag <> "" Then
				'txtOHPON.Text = myform.Tag
				'myform.Close()
				WriteToScreen()
				ButtonStatus(("REV"))

			End If
		End Sub
		Sub WriteToScreen()
			Dim i As Object
			Dim StrCV As Object
			On Error Resume Next
			If NewTrans Then ' tao moi
				cboOHPOT.SelectedIndex = 0
				txtOHPON.Text = ""
				txtOHPOD.Text = VB6.Format(WorkingDate, "dd/mm/yyyy")
				txtOHRQD.Text = ""
				txtOHCNO.Text = ""
				txtCMNME.Text = ""
				txtOHVDN.Text = ""
				txtVMNME.Text = ""
				cboOHCPO.Text = ""
				cboOHPOS.SelectedIndex = 0
				cboOHCUR.defText = ""
				txtOHCUR.Text = ""
				txtOHEXR.Text = CStr(0)
				txtOHTAM.Text = CStr(0)
				txtOHBTA.Text = CStr(0)
			End If

			Dim recPOL As ADODB.Recordset
			If ISADDING Then ' them moi
				txtOLITN.Text = ""
				txtOLLDT.Text = VB6.Format(WorkingDate, "dd/mm/yyyy")
				txtOLITD.Text = ""
				txtOLPOL.Text = CStr(0)
				txtOLODQ.Text = CStr(0)
				txtOLODW.Text = CStr(0)
				txtOLUMP.Text = CStr(0)
				txtOLLAM.Text = ""
				cboOLPER.Text = ""
				txtOLBLA.Text = ""
				cboOLSTT.SelectedIndex = 0
				StrCV = ""
				txtOLRCQ.Text = CStr(0)
				txtOLRCW.Text = CStr(0)
				txtOLPDC.Text = CStr(0)
			Else 'Sua chua
				'FROM AVM LEFT JOIN IIM ON AVM.VMVDN = IIM.IMVDC
				If txtOLPOL.Text <> "" Then
					recPOL = DBconn.Execute("select POL.*,POH.* from POL LEFT JOIN POH ON POL.OLPON=POH.OHPON where POL.OLPON='" & txtOHPON.Text & "' and OLPOL='" & txtOLPOL.Text & "'")
				Else
					recPOL = DBconn.Execute("select POL.*,POH.* from POL LEFT JOIN POH ON POL.OLPON=POH.OHPON where POL.OLPON='" & txtOHPON.Text & "' Order by POL.OLPOL")
				End If
				If Not recPOL.EOF Then
					With recPOL
						cboOHPOT.SelectedIndex = Val(.Fields("OHPOT").Value) - 1
						txtOHPON.Text = .Fields("OHPON").Value
						OldOHPON = txtOHPON.Text

						txtOHPOD.Text = VB6.Format(.Fields("OHPOD").Value, "dd/mm/yyyy")
						If IsDBNull(.Fields("OHRQD").Value) Then
							txtOHRQD.Text = ""
						Else
							txtOHRQD.Text = VB6.Format(.Fields("OHRQD").Value, "dd/mm/yyyy")
						End If
						txtOHCNO.Text = .Fields("OHCNO").Value
						txtCMNME.Text = Find("RCM", "CMNME", " WHERE CMCNO='" & txtOHCNO.Text & "'")
						txtOHVDN.Text = .Fields("OHVDN").Value
						txtVMNME.Text = Find("AVM", "VMNME", " WHERE VMVDN='" & txtOHVDN.Text & "'")
						cboOHCPO.SelectedIndex = Val(.Fields("OHCPO").Value) - 1
						For i = 0 To cboOHPOS.Items.Count - 1
							If VB.Left(VB6.GetItemString(cboOHPOS, i), 1) = .Fields("OHPOS").Value Then
								cboOHPOS.SelectedIndex = i
								Exit For
							End If
						Next i

						cboOHCUR.defText = .Fields("OHCUR").Value
						txtOHCUR.Text = .Fields("OHCUR").Value
						txtOHEXR.Text = .Fields("OHEXR").Value
						txtOHTAM.Text = .Fields("OHTAM").Value
						txtOHBTA.Text = .Fields("OHBTA").Value
						txtOLITN.Text = .Fields("OLITN").Value
						'OldOLITN = txtOLITN
						txtOLITD.Text = Find("IIM", "IMITD", "Where IMITN='" & txtOLITN.Text & "'")
						txtOLPOL.Text = .Fields("OLPOL").Value
						txtOLODQ.Text = .Fields("OLODQ").Value
						txtOLODW.Text = .Fields("OLODW").Value
						txtOLUMP.Text = .Fields("OLUMP").Value
						txtOLPDC.Text = .Fields("OLPDC").Value
						txtOLLAM.Text = .Fields("OLLAM").Value
						cboOLPER.SelectedIndex = Val(.Fields("OLPER").Value) - 1
						txtOLBLA.Text = .Fields("OLBLA").Value
						cboOLSTT.SelectedIndex = Val(.Fields("OLSTT").Value) - 1
						txtOLRCQ.Text = .Fields("OLRCQ").Value
						txtOLRCW.Text = .Fields("OLRCW").Value
						txtOLLDT.Text = IIf(IsDBNull(.Fields("OLLDT").Value), "", VB6.Format(.Fields("OLLDT").Value, "dd/mm/yyyy"))
					End With
				End If
			End If

			Adodc4.RecordSource = "select POL.*,POH.* from POL " & " LEFT JOIN POH ON POL.OLPON=POH.OHPON " & " where POL.OLPON='" & txtOHPON.Text & "'" & " Order by OLPOL"

			Adodc4.Refresh()
		End Sub



		Private Sub cmdSave_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdSave.Click
			'Kiem tra du lieu truoc khi luu
			If Check() = False Then
				Exit Sub
			End If
			WriteToFile()
			NewTrans = False
			ISADDING = False
			ISEDITING = False
			ButtonStatus(("SV"))
		End Sub

		Private Sub frmPUR500_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
			NewTrans = False
			ISEDITING = False
			ISADDING = False
			OpenFile()
			VB6_AddADODataBinding()
			cboOHCNO.RowSource = ADOBind_Adodc1.DataSource
			cboOHVDN.RowSource = ADOBind_Adodc2.DataSource
			cboOHCUR.RowSource = ADOBind_Adodc5.DataSource
			cboOLITN.RowSource = ADOBind_Adodc3.DataSource
			ButtonStatus(("start"))
			Dim myDataFields As String() = {"OLPOL", "OLITN", "OLITD", "CMNME", "OLODQ", "OLRCQ", "OLODW", "OLRCW", "OLUMP", "OLLAM", "OLBLA", "OLSTT"}
			Dim myDataCaptions As String() = {"line", "Item", "Item Description", "Qty.", "RC. Qty.", "Wgt.", "RC. Wgt.", "Unit price", "Price per", "Line amount", "B line amount", "Status"}
			setDataGridView(myDataCaptions, myDataFields, grdPOL)
		End Sub

		Private Sub grdPOL_ClickEvent(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles grdPOL.ClickEvent
			If Adodc4.Recordset.AbsolutePosition = -1 Then
				WriteToScreen()
				Exit Sub
			End If
			txtOLPOL.Text = grdPOL.Columns(1).value
			WriteToScreen()
			If SELECTROW = "" Then SELECTROW = 1
			Adodc4.Recordset.AbsolutePosition = SELECTROW
		End Sub
		Private Sub grdPOL_RowColChange(ByVal eventSender As System.Object, ByVal eventArgs As AxMSDataGridLib.DDataGridEvents_RowColChangeEvent) Handles grdPOL.RowColChange
			If IsNothing(eventArgs.lastRow) Then
				WriteToScreen()
				Exit Sub
			End If
			SELECTROW = Adodc4.Recordset.Bookmark
		End Sub
		Private Sub txtOHCNO_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtOHCNO.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			Dim Result As String
			If KeyAscii = 13 Then
				Result = Find("RCM", "CMNME", "WHERE CMCNO='" & txtOHCNO.Text & "'")
				If Result = "" Then
					cboOHCNO.Visible = True
					cboOHCNO.Focus()
				Else
					cboOHCNO.Visible = False
					txtCMNME.Text = Result
					cboOHCPO.Focus()
				End If
			End If
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub

		Private Sub txtOHEXR_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtOHEXR.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			If KeyAscii = 13 Then
				txtOLITN.Focus()
			End If
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub

		Private Sub txtOHPOD_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtOHPOD.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			If KeyAscii = 13 Then
				txtOHRQD.Focus()
			End If
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub

		Private Sub txtOHPON_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtOHPON.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			Dim Result As Object
			If KeyAscii = 13 Then
				If Not IsNumeric(txtOHPON.Text) Then
					MsgBox("Input string of Digit", MsgBoxStyle.Critical, "Warning")
					txtOHPON.Focus()
					GoTo EventExitSub
				End If
				If txtOHPON.Text = "" Then
					MsgBox("Not let blank PO")
					GoTo EventExitSub
				Else
					Result = Find("POH", "OHPON", "WHERE OHPON='" & txtOHPON.Text & "'")
					If Result = "" Then
						txtOHPOD.Focus()
					Else 'da ton tai
						MsgBox("Doc Number is already exist")
						txtOHPON.Focus()
						GoTo EventExitSub
					End If
				End If
			End If
EventExitSub:
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub

		Private Sub txtOHPON_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtOHPON.Leave
			'If Not IsNumeric(txtOHPON.Text) Then
			'MsgBox "Input string of Digit"
			'txtOHPON.SetFocus
			'End If
		End Sub

		Private Sub txtOHRQD_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtOHRQD.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			If KeyAscii = 13 Then
				txtOHCNO.Focus()
			End If
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub

		Private Sub txtOHVDN_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtOHVDN.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			Dim Result As String
			If KeyAscii = 13 Then
				Result = Find("AVM", "VMNME", "WHERE VMVDN='" & txtOHVDN.Text & "'")
				If Result = "" Then
					cboOHVDN.Visible = True
					cboOHVDN.Focus()
				Else
					cboOHVDN.Visible = False
					txtVMNME.Text = Result
					cboOHPOS.Focus()
				End If
			End If
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub

		Private Sub txtOLITN_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtOLITN.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			Dim Result As String
			If KeyAscii = 13 Then
				Result = Find("IIM", "IMITD", "WHERE IMITN='" & txtOLITN.Text & "'")
				If Result = "" Then
					cboOLITN.Visible = True
					cboOLITN.Focus()
				Else
					txtOLITD.Text = Result
					cboOLITN.Visible = False
					txtOLODQ.Focus()
				End If

			End If
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub
		Sub OpenFile()
			Dim DelStatus As Object
			Dim FieldDel As Object
			Adodc1.ConnectionString = ConnectString
			Adodc1.ConnectionTimeout = 10
			Adodc1.CommandType = ADODB.CommandTypeEnum.adCmdText
			Adodc1.RecordSource = "Select * from RCM"
			Adodc1.Refresh()

			Adodc2.ConnectionString = ConnectString
			Adodc2.ConnectionTimeout = 10
			Adodc2.CommandType = ADODB.CommandTypeEnum.adCmdText
			Adodc2.RecordSource = "Select * from AVM"
			Adodc2.Refresh()

			FieldDel = "IMRID"
			DelStatus = "IZ"
			sql = "Select * from IIM where " & FieldDel & " <>'" & DelStatus & "' " & "order by imitd"
			Adodc3.ConnectionString = ConnectString
			Adodc3.ConnectionTimeout = 10
			Adodc3.CommandType = ADODB.CommandTypeEnum.adCmdText
			Adodc3.RecordSource = sql
			Adodc3.Refresh()

			Adodc4.ConnectionString = ConnectString
			Adodc4.ConnectionTimeout = 10
			Adodc4.CommandType = ADODB.CommandTypeEnum.adCmdText
			Adodc4.RecordSource = "Select POL.*,POH.* from POL " & " LEFT JOIN POH ON POL.OLPON=POH.OHPON where POH.OHPON ='" & txtOHPON.Text & "'"
			Adodc4.Refresh()

			Adodc5.ConnectionString = ConnectString
			Adodc5.ConnectionTimeout = 10
			Adodc5.CommandType = ADODB.CommandTypeEnum.adCmdText
			Adodc5.RecordSource = "Select * from CCM"
			Adodc5.Refresh()

			' Create array from combobox Custtype,StatusOrder, ValuePer
			CustType(1) = "1 Production"
			CustType(2) = "2 Local Purchasing"
			CustType(3) = "3 Import"
			StaOrd(1) = "1 New"
			StaOrd(2) = "2 Modifine"
			StaOrd(3) = "3 Shop Order"
			StaOrd(4) = "4 Started Product"
			StaOrd(5) = "5 Completed"
			StaOrd(6) = "6 Cancel"
			Per(1) = "Unt"
			Per(2) = "Wgt"
			Per(3) = "All"
			POType(1) = "Product"
			POType(2) = "Local Purchasing"
			POType(3) = "Import"

		End Sub

		Sub ButtonStatus(ByRef tdisplay As Object)

			If tdisplay = "edit" Then
				oldReview = cmdReview.Enabled
				oldAddNew = cmdAddNew.Enabled
				oldPreview = CmdPreview.Enabled
				oldNewItem = cmdNewItem.Enabled
				oldEdit = cmdEdit.Enabled
				oldSave = cmdSave.Enabled
				oldCancel = cmdCancel.Enabled
				olddelline = cmddelline.Enabled
			End If
			Select Case LCase(tdisplay)

				Case "start"
					cmdReview.Enabled = True
					cmdAddNew.Enabled = True
					CmdPreview.Enabled = False
					cmdNewItem.Enabled = False
					cmdEdit.Enabled = False
					cmdSave.Enabled = False
					cmdCancel.Enabled = False
					cmddelline.Enabled = False
				Case "rev" 'Review
					cmdReview.Enabled = True
					cmdAddNew.Enabled = True
					CmdPreview.Enabled = True
					cmdNewItem.Enabled = True
					cmdEdit.Enabled = True
					cmdSave.Enabled = False
					cmdCancel.Enabled = False
					cmddelline.Enabled = True

				Case "edit"	' new purchase order
					cmdReview.Enabled = False
					cmdAddNew.Enabled = False
					CmdPreview.Enabled = False
					cmdNewItem.Enabled = False
					cmdEdit.Enabled = False
					cmdSave.Enabled = True
					cmdCancel.Enabled = True
					cmddelline.Enabled = False
				Case "pre" 'Preview Purchase order
					cmdReview.Enabled = True
					cmdAddNew.Enabled = True
					CmdPreview.Enabled = True
					cmdNewItem.Enabled = True
					cmdEdit.Enabled = True
					cmdSave.Enabled = False
					cmdCancel.Enabled = False
					cmddelline.Enabled = True
				Case "sv" 'Save
					cmdReview.Enabled = True
					cmdAddNew.Enabled = True
					CmdPreview.Enabled = True
					cmdNewItem.Enabled = True
					cmdEdit.Enabled = True
					cmdSave.Enabled = False
					cmdCancel.Enabled = False
					cmddelline.Enabled = True
				Case "cl" 'cancel
					cmdReview.Enabled = oldReview
					cmdAddNew.Enabled = oldAddNew
					CmdPreview.Enabled = oldPreview
					cmdNewItem.Enabled = oldNewItem
					cmdEdit.Enabled = oldEdit
					cmdSave.Enabled = oldSave
					cmdCancel.Enabled = oldCancel
					cmddelline.Enabled = olddelline


				Case "cp" 'Close purchase order
					cmdReview.Enabled = True
					cmdAddNew.Enabled = True
					CmdPreview.Enabled = True
					cmdNewItem.Enabled = False
					cmdEdit.Enabled = False
					cmdSave.Enabled = False
					cmdCancel.Enabled = False
					cmddelline.Enabled = False
				Case "clne"	'Close line
					cmdReview.Enabled = True
					cmdAddNew.Enabled = True
					CmdPreview.Enabled = True
					cmdNewItem.Enabled = True
					cmdEdit.Enabled = True
					cmdSave.Enabled = False
					cmdCancel.Enabled = False
					cmddelline.Enabled = False
			End Select

			FramePO.Enabled = NewTrans Or ISEDITING
			FramePOLine.Enabled = ISEDITING
		End Sub

		Sub WriteToFile()
			Dim WGT As Object
			Dim Unt As Object
			Dim OLD0HPON As Object
			Dim txtOHLDT As Object
			On Error GoTo errdesc
			If Check() = False Then
				Exit Sub
			End If
			Dim reqdte As String
			If NewTrans Then ' nhap moi them vao POH
				txtOHLDT = VB6.Format(Now, "yyyy-mm-dd hh:mm:ss")
				txtOHPOD.Text = VB6.Format(WorkingDate, "yyyy-mm-dd hh:mm:ss")
				If txtOHRQD.Text = "" Then
					reqdte = "null"
				Else
					reqdte = "'" & VB6.Format(txtOHRQD.Text, "yyyy-mm-dd hh:mm:ss") & "'"

				End If

				sql = "Insert into POH (OHLDT,OHPOD,OHRQD,OHRID" & ",OHPON,OHCNO,OHVDN" & ",OHCPO,OHPOS,OHCUR,OHPOT" & ",OHEXR,OHTAM,OHBTA) values " & "('" & txtOHLDT & "','" & txtOHPOD.Text & "'," & reqdte & ",'OM'" & ",'" & txtOHPON.Text & "','" & txtOHCNO.Text & "','" & txtOHVDN.Text & "'" & ",'" & cboOHCPO.SelectedIndex + 1 & "','" & cboOHPOS.SelectedIndex + 1 & "','" & cboOHCUR.CtlText & "','" & cboOHPOT.SelectedIndex + 1 & "'" & "," & txtOHEXR.Text & "," & txtOHTAM.Text & "," & txtOHBTA.Text & ")"
				'MsgBox sql
				DBconn.Execute(sql)
			End If
			Dim recPOL As ADODB.Recordset
			If ISADDING Then ' nhap moi them vao POl
				recPOL = DBconn.Execute("Select * from POL where OLPON='" & txtOHPON.Text & "' order by OLPOL Desc ")
				If recPOL.EOF Then
					txtOLPOL.Text = CStr(1)
				Else
					txtOLPOL.Text = recPOL.Fields("OLPOL").Value + 1
				End If
				'FORMAT LAI NGAY GIO TRONG BANG POL
				If txtOLLDT.Text = "" Then
					reqdte = "null"
				Else
					reqdte = "'" & VB6.Format(txtOLLDT.Text, "yyyy-mm-dd hh:mm:ss") & "'"

				End If
				sql = "Insert into POL(OLLDT,OLITN,OLITD,OLRID,OLBLA,OLLUS" & ",OLUMP,OLLAM,OLPER,OLSTT" & ",OLPOL,OLODQ,OLODW,OLPON,OLPDC,OLRCQ,OLRCW) values " & "(" & reqdte & ",'" & txtOLITN.Text & "','" & txtOLITD.Text & "','OM'," & txtOLBLA.Text & ",'" & UserLogin & "'" & ",'" & txtOLUMP.Text & "','" & txtOLLAM.Text & "','" & cboOLPER.SelectedIndex + 1 & "','" & cboOLSTT.SelectedIndex + 1 & "'" & ",'" & txtOLPOL.Text & "'," & txtOLODQ.Text & "," & txtOLODW.Text & ",'" & txtOHPON.Text & "'," & txtOLPDC.Text & " ,0,0)"
				'MsgBox sql
				DBconn.Execute(sql)
				'WriteToIIM txtOLODW, txtOLODQ
				WriteToITH(txtOLODW, txtOLODQ)
			ElseIf ISDELETED Then  'delete hang
				Exit Sub
			ElseIf ISEDITING Then  'Sua chua du lieu

				sql = "Update POH SET  OHLDT='" & txtOHLDT & "',OHPOD='" & VB6.Format(txtOHPOD.Text, "mm/dd/yy") & "',OHRQD=" & IIf(txtOHRQD.Text = "", "Null", "'" & VB6.Format(txtOHRQD.Text, "mm/dd/yy") & "'") & ",OHPON='" & txtOHPON.Text & "',OHCNO='" & txtOHCNO.Text & "',OHVDN='" & txtOHVDN.Text & "'" & ",OHCPO='" & cboOHCPO.SelectedIndex + 1 & "',OHPOS='" & VB.Left(VB6.GetItemString(cboOHPOS, cboOHPOS.SelectedIndex), 1) & "',OHCUR='" & cboOHCUR.CtlText & "',OHPOT='" & cboOHPOT.SelectedIndex + 1 & "'" & ",OHEXR=" & txtOHEXR.Text & ",OHTAM=" & txtOHTAM.Text & ",OHBTA=" & txtOHBTA.Text & " WHERE OHPON='" & OldOHPON & "'"
				'MsgBox sql
				DBconn.Execute(sql)


				'Truong hop thay doi ma so Order
				If txtOHPON.Text <> OldOHPON Then
					sql = "UPDATE POL SET OLPON='" & txtOHPON.Text & "'" & " WHERE OLOPN='" & OLD0HPON & "'"
				End If
				recPOL = DBconn.Execute("Select * from POL where OLPON='" & txtOHPON.Text & "' and OLPOL='" & txtOLPOL.Text & "'")
				' Kiem tra co thay doi ma item moi khong
				If txtOLITN.Text <> recPOL.Fields("OLITN").Value Then
					Unt = Val(txtOLODQ.Text)
					WGT = Val(txtOLODW.Text)
					Unt = IIf(IsNumeric(Unt), Unt, 0)
					WGT = IIf(IsNumeric(WGT), WGT, 0)


					sql = "Update POL set OLLDT=" & IIf(txtOLLDT.Text = "", "Null", "'" & VB6.Format(txtOLLDT.Text, "mm/dd/yy") & "'") & ",OLITN='" & txtOLITN.Text & "',OLITD='" & txtOLITD.Text & "',OLRID='OM',OLBLA=" & txtOLBLA.Text & ",OLLUS='" & UserLogin & "'" & ",OLUMP='" & txtOLUMP.Text & "',OLLAM='" & txtOLLAM.Text & "',OLPER='" & cboOLPER.SelectedIndex + 1 & "',OLSTT='" & cboOLSTT.SelectedIndex + 1 & "'" & ",OLPOL='" & txtOLPOL.Text & "',OLODQ =" & txtOLODQ.Text & ",OLODW=" & txtOLODW.Text & ",OLPDC= " & txtOLPDC.Text & " WHERE OLPON='" & txtOHPON.Text & "' AND OLPOL='" & txtOLPOL.Text & "'"
					'MsgBox sql
					DBconn.Execute(sql)

				Else
					Unt = Val(txtOLODQ.Text) - recPOL.Fields("OLODQ").Value
					WGT = Val(txtOLODW.Text) - recPOL.Fields("OLODW").Value
					sql = "Update POL set OLLDT=" & IIf(txtOLLDT.Text = "", "Null", "'" & VB6.Format(txtOLLDT.Text, "mm/dd/yy") & "'") & ",OLITN='" & txtOLITN.Text & "',OLITD='" & txtOLITD.Text & "',OLRID='OM',OLBLA=" & txtOLBLA.Text & ",OLLUS='" & UserLogin & "'" & ",OLUMP='" & txtOLUMP.Text & "',OLLAM='" & txtOLLAM.Text & "',OLPER='" & cboOLPER.SelectedIndex + 1 & "',OLSTT='" & cboOLSTT.SelectedIndex + 1 & "'" & ",OLPOL='" & txtOLPOL.Text & "',OLODQ =" & txtOLODQ.Text & ",OLODW=" & txtOLODW.Text & ",OLPDC= " & txtOLPDC.Text & " WHERE OLPON='" & txtOHPON.Text & "' AND OLPOL='" & txtOLPOL.Text & "'"
					'MsgBox sql
					DBconn.Execute(sql)
				End If
				WriteToIIM(WGT, Unt)
			End If
			recPOL.Close()
			Adodc4.RecordSource = "select POL.*,POH.* from POL " & " LEFT JOIN POH ON POL.OLPON=POH.OHPON " & " where POL.OLPON='" & txtOHPON.Text & "'" & " Order by OLPOL"
			Adodc4.Refresh()
errexit:
			Exit Sub
errdesc:
			MsgBox(Err.Description)
			GoTo errexit
		End Sub

		Private Sub txtOLODW_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtOLODW.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			If KeyAscii = 13 Then txtOLUMP.Focus()
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub

		Private Sub txtOLODQ_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtOLODQ.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			Dim RecIIM As ADODB.Recordset
			If KeyAscii = 13 Then
				txtOLODW.Focus()

				RecIIM = DBconn.Execute("Select IMWPU from IIM where IMITN='" & txtOLITN.Text & "'")
				If Not RecIIM.EOF Then
					txtOLODW.Text = CStr(RecIIM.Fields("IMWPU").Value * CDbl(txtOLODQ.Text))
				Else
					txtOLODW.Text = CStr(0)
				End If

			End If
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub


		Private Sub txtOLPDC_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtOLPDC.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			If KeyAscii = 13 Then cboOLPER.Focus()
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub

		Private Sub txtOLUMP_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtOLUMP.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			If KeyAscii = 13 Then txtOLPDC.Focus()
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub

		Sub WriteToITH(ByRef WGT As Object, ByRef unit As Object)
			Dim txtTHREM As Object
			Dim txtTHREF As Object
			Dim txtTHCVN As Object
			Dim txtTHCST As Object
			Dim txtTHLTN As Object
			Dim txtTHWHC As Object
			Dim txtTHDTE As Object
			Dim txtTHTTC As Object
			Dim txtTHDOC As Object
			Dim txtTHITN As Object
			Dim txtTHTID As Object
			Dim txtTHTSQ As Object
			Dim Unt As Object

			unit = IIf(IsNumeric(unit), unit, 0)
			WGT = IIf(IsNumeric(WGT), WGT, 0)

			If Val(Unt) = 0 And Val(WGT) = 0 Then
				MsgBox("Quantity or Weight must be >0 ")
				Exit Sub
			End If

			'Lay so THTSQ
			txtTHTSQ = GetTSQ(txtOLITN)
			Dim recITH As ADODB.Recordset
			recITH = DBconn.Execute("Select * from ITH Order by THTID DESC")
			If recITH.EOF Then
				txtTHTID = 1
			Else
				txtTHTID = recITH.Fields("THTID").Value + 1
			End If
			recITH.Close()
			txtTHITN = txtOLITN.Text
			txtTHDOC = txtOHPON.Text
			txtTHTTC = "PO"
			txtTHDTE = VB6.Format(txtOLLDT.Text, "yyyy-mm-dd hh:mm:ss")
			txtTHWHC = ""
			txtTHLTN = ""
			txtTHCST = txtOLUMP.Text
			txtTHCVN = txtOHCNO.Text
			txtTHREF = txtOHPON.Text
			txtTHREM = ""
			txtTHDTE = VB6.Format(txtTHDTE, "yyyy-mm-dd hh:mm:ss")
			sql = "Insert into ITH (THITN,THTSQ,THTID,THDOC,THTTC,THDTE" & ",THWHC,THLTN,THQTY,THWGT,THCST" & ",THCVN,THREF,THREM,THLUS,THLDT) values " & "('" & txtTHITN & "'," & txtTHTSQ & "," & txtTHTID & ",'" & txtTHDOC & "','" & txtTHTTC & "','" & txtTHDTE & "'" & ",'" & txtTHWHC & "','" & txtTHLTN & "'," & unit & "," & WGT & "," & txtTHCST & ",'" & txtTHCVN & "'" & ",'" & txtTHREF & "','" & txtTHREM & "','" & UserLogin & "','" & VB6.Format(Now, "yyyy-mm-dd hh:mm:ss") & "')"
			' MsgBox SQL
			DBconn.Execute(sql)

			Adodc1.Refresh()
		End Sub


		Sub WriteToIIM(ByRef WGT As Object, ByRef unit As Object)
			unit = IIf(IsNumeric(unit), unit, 0)
			WGT = IIf(IsNumeric(WGT), WGT, 0)
			sql = "Update IIM set IMOOU=IMOOU + (" & unit & "),IMOOW= IMOOW + (" & WGT & ") where IMITN='" & txtOLITN.Text & "'"
			DBconn.Execute(sql)
		End Sub

		Sub ClosePO()
			Dim recPOL As ADODB.Recordset
			Dim Unt, Item, Line, WGT, Status As Object
			'Dim recPOH As Recordset
			If (cboOHPOS.SelectedIndex + 1) = 6 Then
				MsgBox("This Purchase order had been delete")
				Exit Sub
			Else

				recPOL = DBconn.Execute("Select * from POL where OLPON='" & txtOHPON.Text & "' and OLSTT<>'6'")
				With recPOL
					Do While Not .EOF
						Line = .Fields("OLPOL").Value
						Item = .Fields("OLITN").Value
						Status = .Fields("OLSTT").Value
						' Chi dong nhung Purchase nao chua hoan thanh
						If Status <> 6 And Status <> 5 Then
							CloseLine(Item, Line, WGT, Unt, Status)
						End If
						.MoveNext()
					Loop
					.Close()
				End With
				DBconn.Execute("update POH set OHPOS=5 WHERE OHPON='" & txtOHPON.Text & "'")
			End If
		End Sub
		Sub CloseLine(ByRef Item As Object, ByRef Line As Object, ByRef WGT As Object, ByRef Unt As Object, ByRef Status As Object)
			If Status <> 6 And Status <> 5 Then
				DBconn.Execute("update pol set OLSTT=5" & " WHERE OLPON='" & txtOHPON.Text & "' AND OLPOL='" & Line & "'")
			Else
				MsgBox("This line had been finished or deleted")
			End If
		End Sub

	End Class
End Namespace